(function (vc) {

    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addBlacklistInfo: {
                blacklistId: '',
                blacklistName: '',
                startTime: '',
                endTime: '',
                remark: '',
                photo: '',

            }
        },
        _initMethod: function () {
            vc.component._initAddBlacklistInfo();
        },
        _initEvent: function () {
            vc.on('addBlacklist', 'openAddBlacklistModal', function () {
                $('#addBlacklistModel').modal('show');
            });
        },
        methods: {
            addBlacklistValidate() {
                return vc.validate.validate({
                    addBlacklistInfo: vc.component.addBlacklistInfo
                }, {
                    'addBlacklistInfo.blacklistName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "用户名字不能为空"
                        },
                        {
                            limit: "max",
                            param: "1,100",
                            errInfo: "位置名称不能超过100位"
                        },
                    ],
                    'addBlacklistInfo.startTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "开始时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "不是有效的时间格式"
                        },
                    ],
                    'addBlacklistInfo.endTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "结束时间不能为空"
                        },
                        {
                            limit: "dateTime",
                            param: "",
                            errInfo: "不是有效的时间格式"
                        },
                    ],
                    'addBlacklistInfo.remark': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "备注不能为空"
                        },
                        {
                            limit: "max",
                            param: "1,100",
                            errInfo: "备注不能超过100位"
                        },
                    ],




                });
            },
            saveBlacklistInfo: function () {
                if (!vc.component.addBlacklistValidate()) {
                    vc.toast(vc.validate.errInfo);

                    return;
                }

                vc.component.addBlacklistInfo.communityId = vc.getCurrentCommunity().communityId;
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, vc.component.addBlacklistInfo);
                    $('#addBlacklistModel').modal('hide');
                    return;
                }

                vc.http.apiPost(
                    'blacklist.saveBlacklist',
                    JSON.stringify(vc.component.addBlacklistInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            //关闭model
                            $('#addBlacklistModel').modal('hide');
                            vc.component.clearAddBlacklistInfo();
                            vc.emit('blacklistManage', 'listBlacklist', {});

                            return;
                        }
                        vc.message(json);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.message(errInfo);

                    });
            },
            clearAddBlacklistInfo: function () {
                vc.component.addBlacklistInfo = {
                    blacklistName: '',
                    startTime: '',
                    endTime: '',
                    remark: '',
                    photo: ''

                };
            },
            _initAddBlacklistInfo: function () {
                vc.component.addBlacklistInfo.startTime = vc.dateFormat(new Date().getTime());
                $('.addStartTime').datetimepicker({
                    language: 'zh-CN',
                    format: 'yyyy-mm-dd hh:ii:ss',
                    initTime: true,
                    initialDate: new Date(),
                    autoClose: 1,
                    todayBtn: true

                });
                $('.addStartTime').datetimepicker()
                    .on('changeDate', function (ev) {
                        var value = $(".addStartTime").val();
                        vc.component.addBlacklistInfo.startTime = value;
                    });
                $('.addEndTime').datetimepicker({
                    language: 'zh-CN',
                    format: 'yyyy-mm-dd hh:ii:ss',
                    initTime: true,
                    initialDate: new Date(),
                    autoClose: 1,
                    todayBtn: true
                });
                $('.addEndTime').datetimepicker()
                    .on('changeDate', function (ev) {
                        var value = $(".addEndTime").val();
                        vc.component.addBlacklistInfo.endTime = value;
                    });
            },
            _upload: function (event) {
                $("#upload").trigger("click")
            },
            _choose: function (event) {
                var photoFiles = event.target.files;
                if (photoFiles && photoFiles.length > 0) {
                    // 获取目前上传的文件
                    var file = photoFiles[0];// 文件大小校验的动作
                    if (file.size > 1024 * 1024 * 1) {
                        vc.toast("图片大小不能超过 1MB!")
                        return false;
                    }
                    var reader = new FileReader(); //新建FileReader对象
                    reader.readAsDataURL(file); //读取为base64
                    reader.onload = function (e) {
                        vc.component.addBlacklistInfo.photo = reader.result;
                    }
                }
            },
        }
    });

})(window.vc);
